package com.hy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.Result.ResponseResult;
import com.hy.mapper.AppAddressMapper;
import com.hy.mapper.AppHospitaMapper;
import com.hy.mapper.AppHospitalMapper;
import com.hy.pojo.hospital.APPHospital;
import com.hy.pojo.hospital.AppAddress;
import com.hy.pojo.hospital.HospitalWithAddress;
import com.hy.service.AppHospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AppHospitaServiceImpl  implements AppHospitaService {
    @Autowired
    private AppHospitaMapper appHospitalMapper;

    @Autowired
    private AppAddressMapper appAddressMapper;

    //医院列表
    @Override
    public ResponseResult list() {
        List<APPHospital> appHospitals = appHospitalMapper.selectList(null);
        List<HospitalWithAddress> hospitalWithAddressList = new ArrayList<>();
        for (APPHospital appHospital : appHospitals) {
            LambdaQueryWrapper<AppAddress> lm = new LambdaQueryWrapper<>();
            lm.eq(AppAddress::getId, appHospital.getAddressId());
            List<AppAddress> appAddresses = appAddressMapper.selectList(lm);
            HospitalWithAddress hospitalWithAddress = new HospitalWithAddress(appHospital, appAddresses.get(0));

            hospitalWithAddressList.add(hospitalWithAddress);
        }
//        System.err.println(hospitalWithAddressList);

        return ResponseResult.okResult(200,"医院列表",hospitalWithAddressList);
    }
    @Override
    public ResponseResult getHospitalNameById(Long id) {
        try {
            if (id == null) {
                return new ResponseResult(400, "医院ID不能为空", null);
            }

            String hospitalName = appHospitalMapper.getHospitalNameById(id);
            if (hospitalName != null && !hospitalName.isEmpty()) {
                return new ResponseResult(200, "查询成功", hospitalName);
            } else {
                return new ResponseResult(404, "未找到该医院", null);
            }
        } catch (Exception e) {
            return new ResponseResult(500, "查询失败：" + e.getMessage(), null);
        }
    }
}
